package Twitter
{
	import mx.collections.XMLListCollection;
	
	public class Search
	{
		import mx.core.Application;
		import mx.collections.SortField;
		import mx.collections.Sort;
		import mx.collections.ArrayCollection;
		import mx.rpc.events.ResultEvent;
		import mx.managers.CursorManager;
		import mx.utils.Base64Encoder;
		import mx.events.ValidationResultEvent;
		import mx.controls.Alert;
		import mx.utils.ObjectUtil;
		
		import mx.controls.DateField; 
		
		
		import flash.data.SQLConnection;
		import flash.data.SQLStatement;
		import flash.filesystem.File;
		import flash.events.*;
		
		import flash.net.URLLoader;
    	import flash.net.URLLoaderDataFormat;
    	import flash.net.URLRequest;
    	import flash.net.URLVariables;	
    	import flash.net.URLRequestHeader;
    	
    	import mx.utils.StringUtil
		
		import flash.events.SecurityErrorEvent;
		
		public function Search()
		{
		}
		
		
		
		public function get_mentions_user():void
		{
			var reqStr:String = "http://search.twitter.com/search.atom?q=%40" + Application.application._username;
				
		
				
			//trace.show(reqStr);
				
			var request:URLRequest = new URLRequest(reqStr);
			
		
			var loader:URLLoader = new URLLoader();
			

			
			
			loader.addEventListener(Event.COMPLETE, get_user_mentioned);
			
			loader.load(request);
				



		}
		

		
		
		
		public function get_user_mentioned(e:Event):void
		{
			var myHTML:URLLoader = URLLoader(e.target);					
			var myXML:XML = XML(myHTML.data);	
			
			var xmlString:String = myXML.toXMLString();

			// define the regex pattern to remove the namespaces from the string
			var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
			
			// remove the namespaces from the string representation of the XML
			var namespaceRemovedXML:String = xmlString.replace(xmlnsPattern, "");
			
			xmlnsPattern = new RegExp("aaa:lang=\"en-US\"");
			namespaceRemovedXML = namespaceRemovedXML.replace(xmlnsPattern, "");
			
			xmlnsPattern = new RegExp("<twitter:", "gi");
			namespaceRemovedXML = namespaceRemovedXML.replace(xmlnsPattern, "<");
			
			xmlnsPattern = new RegExp("</twitter:", "gi");
			namespaceRemovedXML = namespaceRemovedXML.replace(xmlnsPattern, "</");
			
			xmlnsPattern = new RegExp("<openSearch:", "gi");
			namespaceRemovedXML = namespaceRemovedXML.replace(xmlnsPattern, "<");
			
			xmlnsPattern = new RegExp("</openSearch:", "gi");
			namespaceRemovedXML = namespaceRemovedXML.replace(xmlnsPattern, "</");
			
			// set the string rep. of the XML back to real XML
			var responseXML:XML = new XML(namespaceRemovedXML);
			
			//Alert.show(responseXML);
			
			var tf:tMessageFormat;
						
			var username:String = "@" + Application.application._username;
			
			for each (var item:XML in responseXML..entry)
			{
				if (item.title.toString().substr(0,username.length) != username)
				{
					//tf  = new tMessageFormat();
					//Alert.show("message");
					var o:tStatus = new tStatus();
					
					//try
					{
				
						var id:Array = item.id.toString().split(":");
						
						//check if exist in replies
						
					
						//try
						//{
							var dateArr:Array = item.updated.toString().split("T");
						
							dateArr[1] = dateArr[1].replace("Z","");
						
							var dateparts:Array = dateArr[0].split("-");
						
							var timeparts:Array = dateArr[1].split(":");
						
							var time:Date = new Date(dateparts[0], dateparts[1], dateparts[2], timeparts[0], timeparts[1]);
							
							//var offsetMilliseconds:Number = time.getTimezoneOffset() * 60 * 1000;
							//time.setTime(time.getTime() + offsetMilliseconds);
							
							o.created_at = tf.format_date(time.toString());
							
						//}
						//catch(err:Error)
						//{
							//o.created_at = item.updated.toString();
							
						//}
						
						o.id = id[2];
						o.text = item.title.toString();
						o.screen_name = item.author.uri.toString().replace("http://twitter.com/", "");
						o.profile_image_url = item.link.(@href).toString();
						
						Alert.show(item.link.toString());
						
						Application.application.myReplies.addItemAt(o,0);
						
						var sort:Sort = new Sort();
						sort.fields = [new SortField("id", false, true)];
						Application.application.myStatuses.sort = sort;
						
						}
					//catch(err:Error)
					{
						
					}
					
				
				

					

					//o.screen_name = item.twitter:source.name.toString();
					//o.profile_image_url = item.user.profile_image_url.toString();
					
					//tf.match_keywords(item.text.toString());
					
					//o._key_match = tf._matches_keyword;
					
	
					//Application.application.myReplies.addItemAt(o,0);
					
					/*sort = new Sort();
					sort.fields = [new SortField("id", false, true)];
					Application.application.myStatuses.sort = sort;*/
					
				}
				
			}			
			

		}

	}
}